<?php
/**
 * @package   auto_occasion
 * @subpackage 
 * @author    CMS
 * @copyright  2011
 * @license    All rights reserved
 */

class gammeModeleSrv {

    /**
     * Fonction permettant de sauvegarder les modele d'une gamme
     * @return object $gammeModele
     *
     */
    public function ajouter ($gammeModele) {
        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_modele");
        $records   = jDao::createRecord("commun~essai_gamme_modele");

        $records->essai_gamme_modele_id_modele  = $gammeModele->essai_gamme_modele_id_modele;
        $records->essai_gamme_modele_title      = $gammeModele->essai_gamme_modele_title;
        $records->essai_gamme_modele_content    = $gammeModele->essai_gamme_modele_content;
        $records->essai_gamme_modele_pos        = $gammeModele->essai_gamme_modele_pos;
        $records->essai_gamme_modele_image      = $gammeModele->essai_gamme_modele_image;
        $records->essai_id                      = $gammeModele->essai_id;
        
        if ($maFactory->insert($records)) {
            return $records->essai_gamme_modele_id;
        }

    }
     /**
     * Fonction permettant de modifier les modele d'une gamme
     * @return object $partenaire
     *
     */
    public function modifier ($gammeModele) {
        // instanciation de la factory
        $maFactory = jDao::get("commun~essai_gamme_modele");
        $records   = $maFactory->get($gammeModele->essai_gamme_modele_id);

        $records->essai_gamme_modele_id_modele  = $gammeModele->essai_gamme_modele_id_modele;
        $records->essai_gamme_modele_title      = $gammeModele->essai_gamme_modele_title;
        $records->essai_gamme_modele_content    = $gammeModele->essai_gamme_modele_content;
        $records->essai_gamme_modele_pos        = $gammeModele->essai_gamme_modele_pos;
        $records->essai_gamme_modele_image      = $gammeModele->essai_gamme_modele_image;
        $records->essai_id                      = $gammeModele->essai_id;
        

        if ($maFactory->update($records)) {
            return $gammeModele->essai_gamme_modele_id;
        }

    }
    /**
     * Fonction permettant de sauvegarder les modele d'une gamme
     * @return object $gammeModele
     *
     */
    public function ajouter_ ($gammeModele) {
        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_essais_chapitre");
        $records   = jDao::createRecord("commun~ao_essais_chapitre");

        $records->chap_name_id   = NULL;
        $records->chap_content   = $gammeModele->essai_gamme_modele_content;
        $records->chap_position  = 1;
        $records->chap_img       = $gammeModele->essai_gamme_modele_image;
        $records->type           = 2;
        $records->chap_ess_id    = $gammeModele->essai_id;
        $records->chap_modele    = $gammeModele->essai_gamme_modele_id_modele;
        $records->chap_titre    = $gammeModele->essai_gamme_modele_title;
        
        if ($maFactory->insert($records)) {
            return $records->chap_modele;
        }

    }
     /**
     * Fonction permettant de modifier les modele d'une gamme
     * @return object $partenaire
     *
     */
    public function modifier_ ($gammeModele) {
        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_essais_chapitre");
        $records   = $maFactory->get($gammeModele->chap_id);

        $records->chap_name_id   = NULL;
        $records->chap_content   = $gammeModele->essai_gamme_modele_content;
        $records->chap_position  = 1;
        $records->chap_img       = $gammeModele->essai_gamme_modele_image;
        $records->type           = 2;
        $records->chap_ess_id    = $gammeModele->essai_id;
        $records->chap_modele    = $gammeModele->essai_gamme_modele_id_modele;
        $records->chap_titre    = $gammeModele->essai_gamme_modele_title;

        if ($maFactory->update($records)) {
            return $gammeModele->chap_modele;
        }

    }

    /**
     * Liste gamme modele et tri
     * @param array $_toParams
     * @return array 
     */
    public static function reOrder ($_toParams) {
        
        $oFac = jDao::get('commun~ao_essais_chapitre') ;
        
        $tiMarqueIds = array_key_exists('tiMarqueIds', $_toParams) ? $_toParams['tiMarqueIds'] : array() ;
        
        foreach ($tiMarqueIds as $iKey => $iMarqueId) {
            $oRec = $oFac->get($iMarqueId) ;
            $oRec->chap_position = $iKey ;
            $oFac->update($oRec) ;
        }
        
    }

    /**
     * liste gamme modele par essai
     * @param int $essai_id
     * @return array 
     */
    public static function getAllByEssai ($essai_id) {
        
        $oFac = jDao::get('commun~essai_gamme_modele') ;
        $oCond = jDao::createConditions() ;
        $oCond->addCondition('essai_id', '=', $essai_id) ;
        $oCond->addItemOrder('essai_gamme_modele_pos') ;
        
        return $oFac->findBy($oCond)->fetchAll() ;
    }


}